﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Student
{
    public partial class select_student : Form
    {
        List<Stu_group> groups;
        List<g_data> g_Datas;
        List<g_display> gds;
        Models.t_data m_t;
        List<Models.Student> all_student;
        public select_student()
        {
            InitializeComponent();
        }

        private void student_list_MouseClick(object sender, MouseEventArgs e)
        {
            int idx = group_list.CurrentCell.RowIndex;
            int choice;
            System.Windows.Forms.TextBox selected = selected_box(out choice);

            Group_name.Text = gds[idx].group_name;
            Project_name.Text = gds[idx].project_name;
            Group_info.Text = gds[idx].project_info;

            var g_members = all_student.Where(stu=>stu.group_id== gds[idx].g_id).ToList();
            Group_members.DataSource = g_members;

            selected.Text = gds[idx].project_name;

            for(int i = 1; i <= log_info.Sys_Config.tearch_choice; i++)
            {
                if(choice == i)
                {
                    set_tChoice(m_t, $"c{i}", gds[idx].g_id);
                    break;
                }
            }
        }

        private System.Windows.Forms.TextBox selected_box(out int choice)
        {
            System.Windows.Forms.TextBox selected;

            if (radio_c1.Checked)
            { selected = choice1; choice = 1; }
            else if (radio_c2.Checked)
            { selected = choice2; choice = 2; }
            else if (radio_c3.Checked)
            { selected = choice3; choice = 3; }
            else if (radio_c4.Checked)
            { selected = choice4; choice = 4; }
            else if(radio_c5.Checked)
            { selected = choice5; choice = 5; }
            else if (radio_c6.Checked)
            { selected = choice6; choice = 6; }
            else if (radio_c7.Checked)
            { selected = choice7; choice = 7; }
            else if (radio_c8.Checked)
            { selected = choice8; choice = 8; }
            else if (radio_c9.Checked)
            { selected = choice9; choice = 9; }
            else
            { selected = choice10; choice = 10; }

            return selected;
        }

        private int get_tChoice(Models.t_data t_Data, string pro_name)
        {
            return (int)t_Data.GetType().GetProperty(pro_name).GetValue(t_Data, null); ;
        }

        private void set_tChoice(Models.t_data t_Data, string pro_name, int g_id)
        {
            t_Data.GetType().GetProperty(pro_name).SetValue(t_Data, g_id);
        }

        private void select_student_Load(object sender, EventArgs e)
        {
            BLL.B_g_display b_G_Display = new BLL.B_g_display();
            gds = b_G_Display.GetList();
            group_list.DataSource = gds;

            for(int i = 0;i<9;i++)
                group_list.Columns[i].Visible = false;

            BLL.B_Student b_student = new BLL.B_Student();
            all_student = b_student.GetList();

            B_T b_T = new B_T();
            
            m_t = b_T.select(log_info.teacher);

            if (m_t == null)
            {
                m_t = new Models.t_data();
                return; 
            }

            for(int i = 1;i<=log_info.Sys_Config.tearch_choice;i++)
            {
                var choice = get_tChoice(m_t, $"c{i}");
                if (choice != -1)
                {
                    var txtbox = this.groupBox2.Controls.Find($"choice{i}", false)[0];
                    txtbox.Text = gds.Where(g => g.g_id == choice).First().project_name;
                }
            }
        }

        private void 提交_Click(object sender, EventArgs e)
        {
            if(log_info.sys_Configs[0].teacher_deadline < log_info.Server_Time)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            m_t.t_id = log_info.teacher.teacher_id;
            BLL.B_T b_T = new BLL.B_T();
            if(b_T.commit(m_t))
                MessageBox.Show("ok!");

        }

        private void sTreturn1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (log_info.sys_Configs[0].teacher_deadline < log_info.Server_Time)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            m_t.t_id = log_info.teacher.teacher_id;
            BLL.B_T b_T = new BLL.B_T();
            if (b_T.commit(m_t))
                MessageBox.Show("ok!");
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            // 获取当前选中的单选按钮和对应的文本框
            int choice;
            System.Windows.Forms.TextBox selected = selected_box(out choice);
            selected.Text = ""; // 清除文本框的内容

            // 根据选择更新m_g对象的相应属性为null
            if (choice == 1)
                choice1 = null;
            else if (choice == 2)
                choice2 = null;
            else if (choice == 3)
                choice3 = null;
            else if (choice == 4)
                choice4 = null;
            else if (choice == 5)
                choice5 = null;
            else if (choice == 6)
                choice6 = null;
            else if (choice == 7)
                choice7 = null;
            else if (choice == 8)
                choice8 = null;
            else if (choice == 9)
                choice9 = null;
            else if (choice == 10)
                choice10 = null;
        }
    }
}
